<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2020/1/11
 * Time: 10:41
 */

namespace app\index\controller;
use think\captcha\Captcha;
use think\Cookie;

class VerifyCode
{
    public function getCode(){
        //判断当前客户端是否有一个验证码标识 ,如果有则使用现有,如果没有则直接生成一个新的
        if(!isset($_COOKIE['verify_sign'])){
            //生成一个客户端验证码的标识码
            $sign = md5(time().mt_rand(1000,9999));
            cookie('verify_sign',$sign,30*24*60*60);
        }else{
            $sign = Cookie::get('verify_sign');
        }

        $captcha = new Captcha();
        return $captcha->entry($sign);
    }

    public function checkCode(){
        echo '正在验证验证码,sign:'.Cookie::get("verify_sign");
        $code = input("code");
        echo '输入的验证码为:'.$code;

        $captcha = new Captcha();
        $sign = Cookie::get('verify_sign');
        echo '<br/>';
        if($captcha->check($code, $sign)){
            echo '验证码输入正确!';
        }else{
            echo '验证码输入错误!';
        }
    }
}